import java.util.Vector;
public class EnemyTank extends Tank implements Runnable {

    public EnemyTank(int x, int y) {
        super(x, y);
    }
    Vector<Shot> Shots = new Vector<>();//存放当前坦克的子弹集合

    @Override
    public void run() {
        /**
         * 下面做子弹以及方向的随机
         */
        while (isLive) {
            if(Shots.size()< Constant.MAX_BULLET_OF_TANK) {
                Shot s = null;
                switch (getDirect()){
                    case 0:
                        s=new Shot(getX()-2,getY()-30,0);
                        break;
                    case 1:
                        s=new Shot(getX()+30,getY()-2,1);
                        break;
                    case 2:
                        s=new Shot(getX()-2,getY()+30,2);
                        break;
                    case 3:
                        s=new Shot(getX()-30,getY()-2,3);
                        break;
                    default:
                        break;
                }
                Shots.add(s);
                new Thread(s).start();
            }
            switch (getDirect()) {
                case 0:
                    for (int i = 0; i < Math.random() * 40 + Constant.ENEMY_ROTATION_FREQUENCY; i++) {
                        if (getY() - 30 > 0)
                            moveUp();
                        try {
                            Thread.sleep(Constant.REFRESH_RATE);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < Math.random() * 40 + Constant.ENEMY_ROTATION_FREQUENCY; i++) {
                        if (getX() + 30 < Constant.GAME_WIDTH)
                            moveRight();
                        try {
                            Thread.sleep(Constant.REFRESH_RATE);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < Math.random() * 40 + Constant.ENEMY_ROTATION_FREQUENCY; i++) {
                        if (getY() + 30 < Constant.GAME_HEIGHT)
                            moveDown();
                        try {
                            Thread.sleep(Constant.REFRESH_RATE);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < Math.random() * 40 + Constant.ENEMY_ROTATION_FREQUENCY; i++) {
                        if (getX() - 30 > 0)
                            moveLeft();
                        try {
                            Thread.sleep(Constant.REFRESH_RATE);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                default:
                    break;
            }

            setDirect((int) (Math.random() * 4));//设置完后为下一次方向做随机选择

        }
    }
}
